package com.mediacloud.app.newsmodule.activity

import android.text.TextUtils
import com.mediacloud.app.assembly.util.toast
import com.mediacloud.app.newsmodule.fragment.baoliao.api.SpiderCmsApi
import com.mediacloud.app.newsmodule.fragment.simulcast.CCTVLiveStatus
import com.mediacloud.app.newsmodule.fragment.simulcast.DateBean
import com.mediacloud.app.newsmodule.fragment.simulcast.SimulcastBean
import com.mediacloud.app.newsmodule.fragment.simulcast.TodayCCTVNews
import com.mediacloud.app.quanzi.model.BaseDataBean
import com.mediacloud.app.quanzi.model.BaseDataListBean
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

/**
 * 获取当日直播新闻的数据
 * */
fun CCTVNewsDetailActivity.getTodayDetail(catalogId: String?) {
    //查询当天的新闻数据
    SpiderCmsApi.getSpiderCmsApi(this).getTodayCCTVNews(catalogId)
        .enqueue(object : Callback<BaseDataBean<TodayCCTVNews>> {
            override fun onResponse(
                call: Call<BaseDataBean<TodayCCTVNews>>,
                response: Response<BaseDataBean<TodayCCTVNews>>
            ) {
                if (response.isSuccessful && response.body()?.isSuccess == true) {
                    val data = response.body()?.data
                    setTodayNews(data)
                } else {
                    toast("未查询到相关数据")
                }
            }


            override fun onFailure(call: Call<BaseDataBean<TodayCCTVNews>>, t: Throwable) {
                toast("未查询到相关数据")
            }

        })
}

/**
 * 查询往期新闻数据
 */
fun CCTVNewsDetailActivity.getSelectDayData(searchDate: String?, catalogId: String?) {

    SpiderCmsApi.getSpiderCmsApi(this)
        .getNewsCastByDate(catalogId.toString(), firstCatalogId, "10137", searchDate)
        .enqueue(object : Callback<BaseDataBean<SimulcastBean>> {

            override fun onResponse(
                call: Call<BaseDataBean<SimulcastBean>>,
                response: Response<BaseDataBean<SimulcastBean>>
            ) {
                if (response.isSuccessful && response.body()?.isSuccess == true) {
                    val data = response.body()?.data
                    setSelectDayNews(data)
                }
            }


            override fun onFailure(call: Call<BaseDataBean<SimulcastBean>>, t: Throwable) {

            }

        })
}

/**
 * 当日新闻联播的状态（可以理解为获取直播文稿的直播状态）
 * */
fun CCTVNewsDetailActivity.getTodayNewsStatus(catalogId: String?) {
    if (TextUtils.isEmpty(catalogId)) {
        toast("未获取到栏目ID")
        return
    }
    SpiderCmsApi.getSpiderCmsApi(this).getCCTVLiveStatus(catalogId)
        .enqueue(object : Callback<BaseDataBean<CCTVLiveStatus>> {
            override fun onResponse(
                call: Call<BaseDataBean<CCTVLiveStatus>>,
                response: Response<BaseDataBean<CCTVLiveStatus>>
            ) {
                if (response.isSuccessful && response.body()?.isSuccess == true) {
                    getTodayNewsStatusSuccess(response.body()?.data)
                } else {
                    getTodayNewsStatusSuccess(null)
                }
            }

            override fun onFailure(call: Call<BaseDataBean<CCTVLiveStatus>>, t: Throwable) {
                getTodayNewsStatusSuccess(null)
            }

        })
}

/**
 * 获取置顶日期区间是否有联播新闻
 * */
fun CCTVNewsDetailActivity.getCatalogDate(
    catalogId: String?,
    startTime: String?,
    endTime: String?,
) {
    SpiderCmsApi.getSpiderCmsApi(this).getCatalogDate(catalogId, startTime, endTime)
        .enqueue(object : Callback<BaseDataBean<List<DateBean>>> {
            override fun onResponse(
                call: Call<BaseDataBean<List<DateBean>>>,
                response: Response<BaseDataBean<List<DateBean>>>
            ) {
                if (response.isSuccessful && response.body()?.isSuccess == true) {
                    val data = response.body()?.data

                }
            }

            override fun onFailure(call: Call<BaseDataBean<List<DateBean>>>, t: Throwable) {

            }

        })
}

/**
 *  通过弹框选择的日期的处理
 * */
fun CCTVNewsDetailActivity.dealSelectDate(data: String) {

    var is7days = false//是否为近7日
    var pos = -1
    for ((index, item) in dateList.withIndex()) {
        if (item.searchTime == data) {
            is7days = true
            pos = index
        }
    }

    if (is7days) {
        if (pos != selectDatePos) {
            if (pos == 0) {
                if (isTodayHaveNews) {
                    selectDatePos = 0
                    changeSelectPos(0)
                    getTodayDetail(todayCatalogId)
                }
            } else {
                changeSelectPos(pos)
                selectDatePos = pos
                getSelectDayData(data, catalogId)
            }
        }
    } else {
        changeSelectPos(-1)
        getSelectDayData(data, catalogId)
    }

}


/**
 * 获取某天的关联新闻（拆条视频）列表
 * */
fun CCTVNewsDetailActivity.getRelatedNews() {

}

/**
 * 获取某天的新闻对应的评论列表
 * */
fun CCTVNewsDetailActivity.getComment() {

}